﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Web;

namespace WCFPointOfSaleService.Class
{
    public static class Validation
    {
        public static bool checkParameterRequired(object parameter)
        {

            if (parameter != null)
            {
                Type type = parameter.GetType();
                PropertyInfo[] properties = parameter.GetType().GetProperties();

                foreach (var propertyInfo in properties)
                {
                    object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(ValidationAttribute), inherit: true);

                    foreach (var customAttribute in customAttributes)
                    {
                        var validationAttribute = (ValidationAttribute)customAttribute;

                        bool isValid = validationAttribute.IsValid(propertyInfo.GetValue(parameter, BindingFlags.GetProperty, null, null, null));

                        if (!isValid)
                        {
                            throw new ParameterException("APP.01", validationAttribute.ErrorMessage);
                        }
                    }
                }
            }
            else
            {
                throw new ParameterException("APP.01", "Parámetros no recibidos");
            }

            return true;

        }

    }
}